package com.dfec.water.dao;

import com.dfec.framework.mybatis.core.mapper.BaseMapperX;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.dfec.water.entity.YdbDeviceTypeEntity;
import com.dfec.water.entity.YdbWarnDeviceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 设备类别维护Dao接口
 *
 * @author df
 * @date Thu Feb 06 11:10:12 CST 2025
 */
@Mapper
public interface YdbWarnDeviceDao extends BaseMapperX<YdbWarnDeviceEntity> {

    /**
     * description:分页查询
     *
     * @param pageParam 分页参数
     * @return
     * @author trg
     * @date 2023/6/27 15:56
     */
    default PageUtils selectPage(PageParam<YdbWarnDeviceEntity> pageParam) {
        YdbWarnDeviceEntity entity = pageParam.getEntity() == null ? new YdbWarnDeviceEntity() :pageParam.getEntity();
        return this.selectPage(pageParam, dealParams(entity));
    }


    /**
     * 不带分页查询
     * @param YdbWarnDeviceEntity
     * @return
     */
    default  List<YdbWarnDeviceEntity> selectAll(YdbWarnDeviceEntity YdbWarnDeviceEntity){
        return this.selectList(dealParams(YdbWarnDeviceEntity));
    }

    /**
     * description:查询条件拼接
     *
     * @param entity 参数
     * @return
     * @author trg
     * @date 2023/6/27 15:56
     */
    public default LambdaQueryWrapperX<YdbWarnDeviceEntity> dealParams(YdbWarnDeviceEntity entity){
        return new LambdaQueryWrapperX<YdbWarnDeviceEntity>()
                .eqIfPresent(YdbWarnDeviceEntity::getDeviceId, entity.getDeviceId())
                .eqIfPresent(YdbWarnDeviceEntity::getThreshold, entity.getThreshold())
                .orderByDesc(YdbWarnDeviceEntity::getCreateTime)
                ;
    }


    /**
     * 获取报警项目
     * @return
     */
    @Select("\tSELECT \n" +
            "\t* \n" +
            "FROM\n" +
            "\tydb_area_base_info area\n" +
            "\twhere\n" +
            "\tEXISTS(\n" +
            "\tselect *from (\n" +
            "\tSELECT * from ydb_area_device ad WHERE\n" +
            "\tEXISTS ( SELECT * FROM ydb_warn_device wd WHERE wd.device_id = ad.device_id )\n" +
            "\t) dev WHERE dev.area_id = area.id\t)")
    List<Map<String,Object>> getWarnProject();

    /**
     * 获取当前项目的报警设备
     * @param projectId 项目id
     * @return
     */
    @Select("select * from ydb_device_type dt left join ydb_area_device ad  on dt.id = ad.device_id where ad.area_id = #{projectId} and   EXISTS (\n" +
            "select * from ydb_warn_device  wd where wd.device_id = ad.device_id\n" +
            ")")
    List<YdbDeviceTypeEntity> getProjectWarnDevice(@Param("projectId") Long projectId);


    @Select("SELECT \n" +
            "wd.id,\n" +
            "wd.device_id deviceId,\n" +
            "wd.threshold,\n" +
            "wd.act_val actVal,\n" +
            "dt.sbmc,\n" +
            "wd.warn_time warnTime,\n" +
            "dt.sbcs,\n" +
            "dt.csfs,\n" +
            "dt.qtyq,\n" +
            "dt.lon,\n" +
            "dt.lat,\n" +
            "dtd.logo,\n" +
            "dtd.name typeName\n" +
            " from ydb_warn_device wd \n" +
            "left join ydb_device_type dt on wd.device_id =dt.id\n" +
            "left join ydb_device_type_dict dtd on dt.dev_type=dtd.code\n" +
            "where wd.device_id=#{deviceId}\n" +
            "order by wd.\"CREATE_TIME\" desc\n ")
    public List<Map<String,Object>> getDeviceData(@Param("deviceId") Long deviceId);

    @Select("SELECT \n" +
            "wd.id,\n" +
            "wd.device_id deviceId,\n" +
            "wd.threshold,\n" +
            "wd.act_val actVal,\n" +
            "wd.warn_time warnTime,\n" +
            "dt.sbmc,\n" +
            "dt.sbcs,\n" +
            "dt.csfs,\n" +
            "dt.qtyq,\n" +
            "dt.lon,\n" +
            "dt.lat,\n" +
            "dtd.logo,\n" +
            "dtd.name typeName\n" +
            " from ydb_warn_device wd \n" +
            "left join ydb_device_type dt on wd.device_id =dt.id\n" +
            "left join ydb_device_type_dict dtd on dt.dev_type=dtd.code\n" +
            "order by wd.\"WARN_TIME\" desc  limit 20 \n ")
    public List<Map<String,Object>> getTopTen();
}
